package com.qumitech.visualnovel.service.property.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.DateUtil;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.datasource.console.PropertyChangeMapper;
import com.qumitech.visualnovel.model.dao.console.PropertyChange;
import com.qumitech.visualnovel.model.dao.console.UserInfo;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.prop.PropChangeRecordAdminDTO;
import com.qumitech.visualnovel.pubdef.dto.prop.PropChangeRecordAdminQueryDTO;
import com.qumitech.visualnovel.pubdef.dto.prop.PropertyOptionsDTO;
import com.qumitech.visualnovel.service.property.PropertyChangeService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class PropertyChangeServiceImpl implements PropertyChangeService {

    @Autowired
    PropertyChangeMapper propertyChangeMapper;

    @Autowired
    DateUtil dateUtil;

    private static final String SHOWID_CONVERT_UID = "/account/admin/query/uid";
    private final String PropTypeConfigListUrl = "/admin/prop/config/detail";
    private final String GiftPropertyId = "gift";

    private final String PropGrantUrl = "/admin/prop";
    private final String PropGrantRecordUrl = "/admin/prop/change/record";

    private final String PropCardCfgUrl = "/admin/prop/card/options";

    private Integer getAccountId(String showId, Integer accountId)
    {
        if(showId==null)
            return accountId;
        JSONArray postArray = new JSONArray();
        postArray.add(Long.parseLong(showId));
        JSONObject postData = new JSONObject();
        postData.put("present_ids", postArray);
        String body = HttpInvokeUtil.httpPostJson(UrlConstant.SERVER_URL + SHOWID_CONVERT_UID, postData.toJSONString(),UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        JSONObject data = (JSONObject)rsp.getData();
        JSONArray listArray = data.getJSONArray("uid_array");
        if (listArray.size() !=0)
        {
            return (Integer) listArray.get(0);
        }
        else
            return null;
    }

    @Override
    public List<PropertyChange> getPropertyByAccount(Integer accountId, String startTime, String propertyId,
                                                     String changeId, int offset, int pageSize, String orderBy,
                                                     String orderType, Boolean fuzzyModel) {
        String[] timeArray = startTime.split("-");
        String tableName = "property_change_" + timeArray[0] + "_" + timeArray[1] + "_" + timeArray[2];
        return propertyChangeMapper.getList(tableName, accountId, propertyId, changeId, offset, pageSize, orderType, orderBy, fuzzyModel);
    }

    @Override
    public int getCountByAccount(Integer accountId, String startTime, String propertyId, String changeId, Boolean fuzzyModel) {
        String[] timeArray = startTime.split("-");
        String tableName = "property_change_" + timeArray[0] + "_" + timeArray[1] + "_" + timeArray[2];
        return propertyChangeMapper.getCount(tableName, accountId, propertyId, changeId, fuzzyModel);
    }

    @Override
    public BaseRsp getPropertyOptions() {
        String url = UrlConstant.SERVER_URL + PropTypeConfigListUrl;
        String body = HttpInvokeUtil.httpGet(url, null, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public List<HashMap<String, Object>> getReasonOptions() {
        return propertyChangeMapper.getReasonOptions();
    }

    @Override
    public BaseRsp rewardProp(PropChangeRecordAdminDTO dto) {
        dto.setOperator(getActiveUserName());
        String url = UrlConstant.SERVER_URL + PropGrantUrl;
        String req = JSONObject.toJSONString(dto);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp listPropChangeRecord(PropChangeRecordAdminQueryDTO queryAdminDTO) {
        String url = UrlConstant.SERVER_URL + PropGrantRecordUrl;
        JSONObject o = JSONObject.parseObject(JSONObject.toJSONString(queryAdminDTO));
        Map<String, Object> req = JSONObject.toJavaObject(o, Map.class);
        String body = HttpInvokeUtil.httpGet(url, req);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp listCardOptions() {
        String url = UrlConstant.SERVER_URL + PropCardCfgUrl;
        String body = HttpInvokeUtil.httpGet(url, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    private String getActiveUserName(){
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();
        return activeUser.getUsername();
    }
}
